# -*- coding:utf-8 -*-


# import multiprocessing
# import time
#
# def process_run(n):
#     time.sleep(1)
#     print("process",n)
#
# for i in range(10):
#     p = multiprocessing.Process(target=process_run,args=(i,))
#     p.start()
"""
import os
from multiprocessing import Process


def show_info(title):
    print(title)
    print('module name:', __name__)
    print('parent process:', os.getppid())
    print('process id', os.getpid())
    print('\n\n')


def f(name):
    show_info("function f")
    print(name)


if __name__ == '__main__':
    show_info("main process line")
    p = Process(target=f, args=("children process",))
    p.start()
"""

# 进程间的通信

# 线程间共享内存空间，进程间只能通过其他方法进行通信
#
# Queue
# 注意这个 Queue 不同于 queue.Queue
# Queue type using a pipe, buffer and thread
# 两个进程的 Queue 并不是同一个，而是将数据 pickle 后传给另一个进程的 Queue
# 用于父进程与子进程之间的通信或同一父进程的子进程之间通信
"""
from multiprocessing import Process, Queue
q = Queue()
def p_put(*args):
    q.put(args)
    print('Has put %s' % args)


def p_get(*args):
    print('%s wait to get...' % args)
    print(q.get())
    print('%s got it' % args)

if __name__ == '__main__':

    p1 = Process(target=p_put, args=('p1',))
    p2 = Process(target=p_get, args=('p2',))
    p1.start()
    p2.start()

"""

from multiprocessing import Pipe, Process


def f(conn):
    conn.send('send by child')
    print('child recv:', conn.recv())
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()	# 获得 Pipe 连接的两端
    p = Process(target=f, args=(child_conn, ))
    p.start()
    print('parent recv:', parent_conn.recv())
    parent_conn.send('send by parent')
    p.join()
